<template>
	<jk-dialog
		v-model="showDialog"
		:title="`${pageType === 'edit' ? '编辑' : pageType === 'add' ? '新增' : '详情'}-络筒采集参数`"
		width="700px"
		:show-content-loading="showLoading"
		:confirm-btn-loading="saveBtnLoading"
		:show-confirm-btn="pageType !== 'detail'"
		:show-cancel-btn="pageType !== 'detail'"
		:show-close-btn="pageType === 'detail'"
		@on-close="onVisibleChange(false)"
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
		@on-confirm="onConfirmBtn"
	>
		<el-form ref="ruleForm" :model="ruleForm" :rules="rules" :hide-required-asterisk="pageType === 'detail' || ruleForm.auditState === 1" label-width="110px" :show-message="false">
			<el-row ref="box1">
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="设备" prop="machineCode">
						<span v-if="pageType === 'detail' || ruleForm.auditState === 1" class="read-only-2">{{ ruleForm.machineCode }}</span>
						<jk-select v-else v-model="ruleForm.machineCode" value-key-name="code" size="medium" class="width-100-percent" :option-list="machineList" placeholder="设备" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="IP" prop="ip">
						<span v-if="pageType === 'detail' || ruleForm.auditState === 1" class="read-only-2">{{ ruleForm.ip }}</span>
						<jk-input v-else v-model="ruleForm.ip" placeholder="IP" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="参数-idg" prop="idg">
						<span v-if="pageType === 'detail' || ruleForm.auditState === 1" class="read-only-2">{{ ruleForm.idg }}</span>
						<jk-input v-else v-model="ruleForm.idg" placeholder="参数-idg" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="参数-post" prop="post">
						<span v-if="pageType === 'detail' || ruleForm.auditState === 1" class="read-only-2">{{ ruleForm.post }}</span>
						<jk-input v-else v-model="ruleForm.post" class="width-100-percent" size="medium" placeholder="参数-post" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="参数-slot" prop="slot">
						<span v-if="pageType === 'detail' || ruleForm.auditState === 1" class="read-only-2">{{ ruleForm.slot }}</span>
						<jk-input v-else v-model="ruleForm.slot" class="width-100-percent" size="medium" placeholder="参数-slot" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="参数-otherIdl">
						<span v-if="pageType === 'detail' || ruleForm.auditState === 1" class="read-only-2">{{ ruleForm.otherIdl }}</span>
						<jk-input v-else v-model="ruleForm.otherIdl" class="width-100-percent" size="medium" placeholder="参数-otherIdl" />
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item class="margin-bottom-10" label="备注">
						<span v-if="pageType === 'detail' || ruleForm.auditState === 1" class="read-only-2">{{ ruleForm.remark }}</span>
						<el-input v-else v-model="ruleForm.remark" :rows="4" type="textarea" placeholder="" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</jk-dialog>
</template>

<script>
    import calHeight from '/src/mixins/cal-dialog-height';
    import {
        machineManagerLtDataParamsCreate,
        machineManagerLtDataParamsUpdate,
        machineManagerLtDataParamsGet,
        productionIotMachineGetAll
    } from '../../../api/machine/ltGatherParamConfig';
    import remoteSearchProductInput from '../../technology/order/components/remote-search-product-input/index';
    export default {
        mixins: [calHeight],
        components: { remoteSearchProductInput },
        props: {
            pageType: {
                type: String,
                default: ''
            },
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            }
        },
        data() {
            return {
                machineList: [],
                tableLoading: false,
                saveBtnLoading: false,
                showDialog: false,
                showLoading: true,
                confirmBtnLoading: false,
                saveDialogState: false,
                yarnNameList: [],
                ruleForm: {
                    machineId: '',
                    machineName: '',
                    machineCode: '',
                    ip: '',
                    idg: '',
                    post: '',
                    slot: '',
                    otherIdl: '',
                    remark: ''
                },
                rules: {
                    ip: [{ required: true, trigger: 'change' }],
                    machineCode: [{ required: true, trigger: 'change' }],
                    idg: [{ required: true, trigger: 'change' }],
                    post: [{ required: true, trigger: 'change' }],
                    slot: [{ required: true, trigger: 'change' }],
                    otherIdl: [{ required: true, trigger: 'change' }]
                }
            };
        },
        methods: {
            onDeleteBtn(scope) {
                this.ruleForm.configItem.splice(scope.rowIndex, 1);
            },
            onAddBtn() {
                this.ruleForm.configItem.push(JSON.parse(JSON.stringify(this.defaultRowData)));
            },
            getParamData() {
                const params = JSON.parse(JSON.stringify(this.ruleForm));
                if (!this.dataId) {
                    params.auditState = 1;
                    delete params.id;
                }
                let machineItem = {};
                machineItem = this.machineList.find(k => k.code === params.machineCode) || {};
                params.machineId = machineItem.id;
                params.machineCode = machineItem.code;
                params.machineName = machineItem.name;
                return params;
            },
            onConfirmBtn() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        this.createAndUpdateRequest();
                    } else {
                        this.$tipsBar(this, 'validator');
                    }
                });
            },
            createAndUpdateRequest() {
                const api = !this.dataId ? machineManagerLtDataParamsCreate : machineManagerLtDataParamsUpdate;
                this.saveBtnLoading = true;
                api(this.getParamData()).then(res => {
                    this.saveBtnLoading = false;
                    if (res.data.status === 200) {
                        this.$tipsBar(this, 'save');
                        this.$emit('on-finish', res.data.res);
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            },
            getDetailRequest() {
                return machineManagerLtDataParamsGet({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        const resData = res.data.res || {};
                        resData.configItem = resData.configItem || [];
                        this.ruleForm = res.data.res;
                    }
                });
            },
            getMachineListRequest() {
                return productionIotMachineGetAll({
                    workshopId: 34,
                    type: 1
                }).then(res => {
                    if (res.data.status === 200) {
                        this.machineList = res.data.res ? res.data.res.map(x => {
                            return {
                                id: x.id,
                                code: x.number,
                                name: x.number
                            };
                        }) : [];
                    }
                });
            },
            async getDependentData() {
                this.showLoading = true;
                this.processList = this.$store.state.user.userProcess.processList || [];
                await this.getMachineListRequest();
                if (this.dataId) await this.getDetailRequest();
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
